package com.llseven.ai.spring.llseven.business.impl;

import cn.hutool.core.util.StrUtil;
import com.llseven.ai.spring.llseven.business.BtnGraphBusiness;
import com.llseven.ai.spring.llseven.config.AppConfig;
import com.llseven.ai.spring.llseven.dto.input.GenerateImageInput;
import com.llseven.ai.spring.llseven.dto.output.BtnImageGenerateOutput;
import com.llseven.ai.spring.llseven.dto.output.BtnImageTaskInfoOutput;
import com.llseven.ai.spring.llseven.enumtype.ModelType;
import com.llseven.ai.spring.llseven.service.BailianChatService;
import com.llseven.ai.spring.llseven.dto.input.GenerateTaskInput;
import com.llseven.ai.spring.llseven.service.QiniuService;
import com.llseven.ai.spring.llseven.util.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author ywb
 * @date 2025-04-16 15:30
 */
@Service
public class BtnGraphBusinessImpl implements BtnGraphBusiness {
    private final BailianChatService bailianChatService;
    private final QiniuService qiniuService;
    private final AppConfig appConfig;
    
    public BtnGraphBusinessImpl(BailianChatService bailianChatService, QiniuService qiniuService) {
        this.bailianChatService = bailianChatService;
        this.qiniuService = qiniuService;
        this.appConfig = new AppConfig();
    }
    @Override
    public BtnImageGenerateOutput generate(GenerateImageInput input, MultipartFile refImg) {
        ModelType modelType = ModelType.fromString(input.getModelType());
        
        String prompt = StrUtil.isEmptyIfStr(input.getPrompt()) ? appConfig.getDefaultPrompt() : input.getPrompt();
        
        // 构建上传路径
        String fileUrl = "ai/" + FileUtil.generateFileName() + FileUtil.getFileSuffix(refImg.getOriginalFilename());
        // 将参考图像上传到AI服务
        String imageUrl = qiniuService.uploadTo(fileUrl, refImg);
        imageUrl = imageUrl.replace("https", "http");
        // Create the image generation task
        String imageGenerationTask = bailianChatService.create(modelType, prompt, input.getNegativePrompt(), imageUrl, input.getStrength());
        return new BtnImageGenerateOutput(imageGenerationTask);
    }
    
    
    @Override
    public BtnImageTaskInfoOutput getTask(GenerateTaskInput input) {
        BtnImageTaskInfoOutput btnImageTaskInfoOutput = new BtnImageTaskInfoOutput();
        String imageGenerationResult = bailianChatService.query(input.getTaskId());
        
        if ("Task not completed or failed".equals(imageGenerationResult)) {
            btnImageTaskInfoOutput.setStatus("WAIT");
        } else {
            btnImageTaskInfoOutput.setStatus("SUCCESS");
            
            String fileUrl = "ai/result/" + FileUtil.generateFileName() + ".png";
  
            
            // Step 1: Upload image to Qiniu
            String resultUrl = qiniuService.uploadFromUrl(fileUrl, imageGenerationResult);// Assuming imageGenerationResult is the local file path or a URL
            resultUrl = resultUrl.replace("https", "http");
            btnImageTaskInfoOutput.setImgUrls(resultUrl);
        }
        
        return btnImageTaskInfoOutput;
    }
    
  
    
  
    
    
}
